1.UI設計
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StockCode_U.aspx.cs" Inherits="Web.LStockCode_U" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 100%;">
<tr>
<td colspan="2">Update New StockCode</td>
</tr>
<tr>
<td class="auto-style1">Code</td>
<td>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">Update StockCode</td>
<td>
<asp:Button ID="btnUpdateStockCode" runat="server" OnClick="btnUpdateByID_Click" Text="Update" Width="83px" />
</td>
</tr>
</table>
<table style="width: 100%;">
<tr>
<td>StockCode</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvStockCodes" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
2.內部設計
using Model;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using MongoDB.Driver.Builders;
namespace Web
{
public partial class LStockCode_U : System.Web.UI.Page
{
private MongoDatabase myDB;
protected void Page_Load(object sender, EventArgs e)
{
List<MongoStockCode> StockCodes = new List<MongoStockCode>();
// 產生 MongoClient 物件
string Link = "mongodb://10.10.10.234:27017";
MongoClient _client = new MongoClient(Link);
//_client.GetServer().Connect();
// 取得 MongoServer 物件
#pragma warning disable CS0618
MongoServer server = _client.GetServer();
// 取得 MongoDatabase 物件
myDB = server.GetDatabase("dbs");
// 取得 Collection
MongoCollection<MongoStockCode> _StockCodes = myDB.GetCollection<MongoStockCode>("StockCode");
foreach (MongoStockCode StockCode in _StockCodes.FindAll())
{
StockCodes.Add(StockCode);
}
gvStockCodes.DataSource = StockCodes;
gvStockCodes.DataBind();
}
protected void btnUpdateByID_Click(object sender, EventArgs e)
{
List<MongoStockCode> StockCodes = new List<MongoStockCode>();
MongoCollection<MongoStockCode> _StockCodes = myDB.GetCollection<MongoStockCode>("StockCode");
var _product = _StockCodes.FindOne(Query.EQ("Code", txtCode.Text));
if (_product != null)
{
_product.Name = txtName.Text;
_StockCodes.Save(_product);
}
foreach (MongoStockCode product in _StockCodes.FindAll())
{
StockCodes.Add(product);
}
gvStockCodes.DataSource = StockCodes;
gvStockCodes.DataBind();
}
}
}
3.將0000 / TEST 修改成 0000 / TEST1
4.顯示修改後狀況